package com.yjt.sousou.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

import androidx.core.content.ContextCompat;

import com.yjt.sousou.R;
import com.yjt.sousou.utils.DensityUtil;

/**
 * 作者：83734
 * 时间：2018/12/11
 * 描述：
 */
public class LabelView extends View {
    private Paint mPaint;
    private Paint mPaintText;
    private Path pathLeft;
    private Path pathRight;
    private int width;
    private int widthMim;
    private int widthMax;
    private int height;
    private int offset;
    private Context mContext;
    private int mTextSize;
    private int stateColor;
    private String mStateStr = "";
    private String mTypeStr = "";

    public LabelView(Context context) {
        super(context);
        init(context);
    }

    public LabelView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public LabelView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }
    private void init(Context context){
        mTextSize = (int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_SP, 12, getResources().getDisplayMetrics());

        mContext = context;
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.FILL);
        mPaintText = new Paint();
        mPaintText.setAntiAlias(true);
        mPaintText.setTextSize(mTextSize);
        mPaintText.setColor(Color.WHITE);

        pathLeft = new Path();
        pathRight = new Path();
        width = DensityUtil.dip2px(context,60);
        widthMim = DensityUtil.dip2px(context,46);
        widthMax = DensityUtil.dip2px(context,106);
        height = DensityUtil.dip2px(context,20);
        offset = DensityUtil.dip2px(context,5);

        stateColor = Color.parseColor("#FE8F02");
    }

    public void setStateStr(String stateStr,String typeStr) {
        this.mTypeStr = typeStr;
        switch (stateStr){
            case "1":
                mStateStr = "未派工";
                stateColor =  Color.parseColor("#0B9AFA");
                break;
            case "2":
                mStateStr = "维修中";
                stateColor =  Color.parseColor("#FE8F02");
                break;
            case "3":
                mStateStr = "待评价";
                stateColor =  Color.parseColor("#8B8F8E");
                break;
            case "4":
                mStateStr = "配件处理";
                stateColor =  Color.parseColor("#8B8F8E");
                break;
            case "5":
                mStateStr = "已完结";
                stateColor =  Color.parseColor("#8B8F8E");
                break;
        }
        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mPaint.setColor(stateColor);
        pathLeft.lineTo(width,0);
        pathLeft.lineTo(widthMim,height);
        pathLeft.lineTo(0,height);
        pathLeft.close();
        canvas.drawPath(pathLeft, mPaint);

        pathRight.moveTo(width,0);
        pathRight.lineTo(width*2,0);
        pathRight.lineTo(widthMax,height);
        pathRight.lineTo(widthMim,height);
        pathRight.close();
        mPaint.setColor(Color.parseColor("#3DC5B1"));
        canvas.drawPath(pathRight, mPaint);

        Paint.FontMetricsInt fontMetrics = mPaintText.getFontMetricsInt();
        int baseline = (height - fontMetrics.bottom - fontMetrics.top) / 2;
        mPaintText.setColor(ContextCompat.getColor(mContext, R.color.white));
        canvas.drawText(mStateStr,widthMim/2-mPaintText.measureText(mStateStr)/2,baseline,mPaintText);
        if(mTypeStr.equals("紧急")){
            mPaintText.setColor(ContextCompat.getColor(mContext, R.color.circle_integral_excellent));
        }else {
            mPaintText.setColor(ContextCompat.getColor(mContext, R.color.white));
        }
        canvas.drawText(mTypeStr,width+offset,baseline,mPaintText);
    }
}
